package com.zjx.shop.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjx.shop.common.utils.PageParams;
import com.zjx.shop.product.entity.VO.AttrGroupRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjx.shop.common.utils.PageUtils;
import com.zjx.shop.product.dao.AttrAttrgroupRelationDao;
import com.zjx.shop.product.entity.AttrAttrgroupRelationEntity;
import com.zjx.shop.product.service.AttrAttrgroupRelationService;

import java.util.List;
import java.util.stream.Collectors;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(PageParams params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Page<>(params.getPage(),params.getSize()),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page.getCurrent(),page.getSize(),(long) page.getRecords().size(),page.getRecords());
    }

    @Override
    public void saveBatch(List<AttrGroupRelationVo> vos) {
        List<AttrAttrgroupRelationEntity> entities = vos.stream().map((vo) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(vo, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        this.saveBatch(entities);
    }

}